package com.liuwei.spring.cloud.junit.leetcode.jdk;

import org.apache.commons.lang3.StringUtils;

import java.util.Stack;

/**
 * https://leetcode.cn/problems/reverse-words-in-a-string/description/?envType=study-plan-v2&envId=leetcode-75
 */
public class StrStack {

    /**
     * 给你一个字符串 s ，请你反转字符串中 单词 的顺序
     * @param args
     */
    public static void main(String[] args) {
        StrStack instance = new StrStack();
        String value = "  hello world  ";
        String result = instance.reverseWords(value);
        System.out.println("result:"+result);
    }

    public String reverseWords(String s) {
        s = s.trim();
        while (s.contains("  ")) {
            s = s.replace("  "," ");
        }
        String[] arrays = s.split(" ");
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < arrays.length; i++) {
            sb.append(arrays[arrays.length-1-i]);
            sb.append(" ");
        }
        return sb.toString().trim();
    }

}
